Implement ostree::checksum_file_from_input
authorFelix Krull <f_krull@gmx.de>
Mon, 2 Sep 2019 11:08:35 +0000 (13:08 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:54 +0000 (12:53 -0400)
rust-bindings/rust/src/functions.rs
rust-bindings/rust/tests/functions/mod.rs

index d3a20ffa392e80766e3214a933470d6c72555a09..a56e0da2e6c36fc6549916afcb0752eb9fe176fd 100644 (file)
@@ -32,3 +32,35 @@ pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
         }
     }
 }
+
+pub fn checksum_file_from_input<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellable>>(
+    file_info: &gio::FileInfo,
+    xattrs: Option<&glib::Variant>,
+    in_: Option<&P>,
+    objtype: ObjectType,
+    cancellable: Option<&Q>,
+) -> Result<Checksum, Box<dyn Error>> {
+    unsafe {
+        let mut out_csum = ptr::null_mut();
+        let mut error = ptr::null_mut();
+        let ret = ostree_sys::ostree_checksum_file_from_input(
+            file_info.to_glib_none().0,
+            xattrs.to_glib_none().0,
+            in_.map(|p| p.as_ref()).to_glib_none().0,
+            objtype.to_glib(),
+            &mut out_csum,
+            cancellable.map(|p| p.as_ref()).to_glib_none().0,
+            &mut error,
+        );
+
+        if !error.is_null() {
+            Err(Box::<glib::Error>::new(from_glib_full(error)))
+        } else if ret == GFALSE {
+            Err(Box::new(glib_bool_error!(
+                "unknown error in ostree_checksum_file_from_input"
+            )))
+        } else {
+            Ok(Checksum::from_glib_full(out_csum))
+        }
+    }
+}
index 196c21b481d93378a720cd0389a0b63967edce34..71b557cb4b9823db396126a722decf8bede9378b 100644 (file)
@@ -1,6 +1,6 @@
 use gio::NONE_CANCELLABLE;
 use glib::Cast;
-use ostree::{checksum_file, ObjectType, RepoFile, RepoFileExt};
+use ostree::{checksum_file, checksum_file_from_input, ObjectType, RepoFile, RepoFileExt};
 use util::TestRepo;
 
 #[test]
@@ -19,3 +19,32 @@ fn should_checksum_file() {
 
     assert_eq!(file.get_checksum().unwrap(), result.to_string());
 }
+
+#[test]
+fn should_checksum_file_from_input() {
+    let repo = TestRepo::new();
+    let commit_checksum = repo.test_commit("test");
+
+    let objects = repo
+        .repo
+        .traverse_commit(&commit_checksum, -1, NONE_CANCELLABLE)
+        .expect("traverse commit");
+    for obj in objects {
+        if obj.object_type() != ObjectType::File {
+            continue;
+        }
+        let (stream, file_info, xattrs) = repo
+            .repo
+            .load_file(obj.checksum(), NONE_CANCELLABLE)
+            .expect("load file");
+        let result = checksum_file_from_input(
+            file_info.as_ref().unwrap(),
+            xattrs.as_ref(),
+            stream.as_ref(),
+            ObjectType::File,
+            NONE_CANCELLABLE,
+        )
+        .expect("checksum file from input");
+        assert_eq!(obj.checksum(), result.to_string());
+    }
+}